/* $RCSFile: TMultiColumnDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/TMultiColumnDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Globalization;

using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.UI.DataTypes;
using AddonNice.Diagnostics;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{
    /// <summary>
    ///  Displayed a table as a multicolumns list in a RadComboBox
    /// </summary>
   public class TMultiColumnDataType : ListDataType
    {
        List<int> colSzArray        =   null;
        List<string> colNameindex   =   null;
        bool multiCol               =   false;
       /// <summary>
       /// Constructor
       /// </summary>
       /// <param name="dataSource"></param>
       /// <param name="dataTextField"></param>
       /// <param name="dataValueField"></param>
       /// <param name="colSzAr"> this list contains the value width of column </param>
       /// <param name="colSInd"> this list contains the name of column you want display</param>
       /// <param name="multi"></param>
        public TMultiColumnDataType(object dataSource, string dataTextField, string dataValueField, 
            List<int> colSzAr, List<string> colSInd, bool multi)
        {

            InnerDataType       = PropertiesDataType.List;
            InnerDataSource     = dataSource;
            DataValueField      = dataValueField;
            DataTextField       = dataTextField;
            colSzArray          = colSzAr;
            colNameindex        = colSInd;
            multiCol            = multi;
            if (multi)
            {
                if (colNameindex.Count != colSzArray.Count)
                {
                    string msg  =   string.Format("[E]TMultiColumnDataType Constructor colNameindex.Count:{0} != colSzArray.Count: {1}",colNameindex.Count,colSzArray.Count);
                    if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( msg,DataTypeTraceSwitch.Sw.Info);
                    throw new ArgumentException(msg);
                }
            }
        }
        
       public override object DataSource
        {
            get
            {
                if ( InnerDataSource == null )
                    OnNeedData(new EventArgs());
                return InnerDataSource;
            }
        }
       
       /// <summary>
       /// Initialize Components
       /// </summary>
        protected override void InitializeComponents()
        {
            RadComboBox dd              =   new RadComboBox();
            dd.Width                    =   controlWidth;
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]TMultiColumnDataType InitializeComponents multiCol:{0}.",multiCol),DataTypeTraceSwitch.Sw.Info);
            if (multiCol)
                dd.ItemTemplate         =   new TableTemplate(InnerDataSource, colSzArray, colNameindex);
   		    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]TMultiColumnDataType InitializeComponents HandleChanges: {0}",HandleChanges),DataTypeTraceSwitch.Sw.Info);
            if ( HandleChanges )
            {
                dd.AutoPostBack         =   true;
                dd.SelectedIndexChanged +=  new RadComboBoxSelectedIndexChangedEventHandler(dd_SelectedIndexChanged);
            }
            if ( ReadOnly )
                dd.Enabled              =   false;
            innerControl                =   dd;
        }

       void dd_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
    		if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]TMultiColumnDataType dd_SelectedIndexChanged sender: {0}",sender),DataTypeTraceSwitch.Sw.Info);
            string oldValue         =   Val;
            ReadDataFromEditControl();
            string newValue         =   Val;
            OnDataTypeEvent(new DataTypeEventArgs(DataTypeEventType.Changed,sender,e,oldValue,newValue));
        }
       
        public override void DataBind()
        {
			try
			{
                RadComboBox dd = (RadComboBox)innerControl;
                dd.Items.Clear();
                dd.ClearSelection();
                dd.DataSource               = DataSource;
                dd.DataValueField           = DataValueField;
                dd.DataTextField            = DataTextField;
                dd.DataBind();
                RadComboBoxItem it = dd.FindItemByValue(Val);
                if (it != null)
                    it.Selected = true;
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]TMultiColumnDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
        }

        public override string ReadDataFromEditControl()
		{
            Control Ctl =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ListDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl is RadComboBox)
            {
                RadComboBox dd      =   (RadComboBox)Ctl;
                if (dd.SelectedItem != null)
                    innerValue      = dd.SelectedItem.Value;
                else innerValue     = string.Empty;
            }
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ListDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }

        public override Control EditControl
        {
            get
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]TMultiColumnDataType EditControl GET.",DataTypeTraceSwitch.Sw.Info);
                if (innerControl == null)
                    InitializeComponents();
                return innerControl;
            }
            set
            {
                if (value is RadComboBox)
                {
                    if ( value != innerControl )
                        innerControl    =   value;
                    RadComboBox dd      =   (RadComboBox)innerControl;
                    if ( HandleChanges )
                    {
                        dd.AutoPostBack         =   true;
                        dd.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(dd_SelectedIndexChanged); 
                    }
                    // DO NOT use Value but use InnerValue
                    if (dd.SelectedItem != null)
                            innerValue = dd.SelectedItem.Value;
                    else    innerValue = string.Empty;
                }
                else
                {
                    string msg  =   string.Format("[E]TMultiColumnDataType EditControl RadCombobox values is required :{0} ", value.GetType().Name);
                    if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( msg,DataTypeTraceSwitch.Sw.Info);
                    throw new ArgumentException(msg);
                }
            }
        }

		public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				innerValue = value;
                if ( innerControl == null )
                    return;
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]TMultiColumnDataType Value Set value: {0}", value),DataTypeTraceSwitch.Sw.Info);
				//Update value in control
				RadComboBoxItem it  =   ((RadComboBox) innerControl).FindItemByValue(value);
                if ( it != null )
                    it.Selected =   true;
			}
		}

        public override string Description
        {
            get
            {
                return "Telerik Combobox List";
            }
        }

        /// <summary>
        ///  Used for radCombobox multicolumn in TCustomListdatatype
        /// </summary>
        class TableTemplate : ITemplate
        {
            List<int> colSzArray = null;
            List<string> colNameArray = null;

            public TableTemplate(object InnerDataSource, List<int> colSzAr, List<string> colSInd)
            {

                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]TMultiColumnDataType TableTemplate Ctor.",DataTypeTraceSwitch.Sw.Info);
                colSzArray = colSzAr;
                colNameArray = colSInd;
            }

            /// <summary>
            /// Instantiate the control
            /// </summary>
            /// <param name="container"></param>
            public void InstantiateIn(Control container)
            {
                Table tab               =   new Table();
                tab.ID                  =   "tab_" + container.ClientID;
                tab.CellPadding         =   2;
                tab.CellSpacing         =   0;

                TableRow r              =   new TableRow();
                if (DataTypeTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]TMultiColumnDataType TableTemplate InstantiateIn colSzindex  : {0}", colNameArray.Count), DataTypeTraceSwitch.Sw.Info);
                for (int i = 0; i < colNameArray.Count; i++)
                {
                        TableCell c     =   new TableCell();
                    /* ??
                        if (i == 0)
                            c.Visible   =   false;
                     */
                        c.Width         = colSzArray[i];
                        Label label     =   new Label();
                        label.ID        =   string.Format("Lbl_{0}",i);
                        c.Controls.Add(label);
                        r.Controls.Add(c);
                }
                tab.Controls.Add(r);
                tab.DataBinding         +=  new EventHandler(tab_DataBinding);
                container.Controls.Add(tab);
            }

            private void tab_DataBinding(object sender, EventArgs e)
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine("[V]TMultiColumnDataType TableTemplate tab_DataBinding BEG.",DataTypeTraceSwitch.Sw.Info);
                Table target            = (Table)sender;
                TableRow rw             = target.Rows[0];

                RadComboBoxItem item    = (RadComboBoxItem)target.BindingContainer;
                for (int i = 0; i < rw.Cells.Count; i++)
                {
                    Label target1 = (Label)rw.Cells[i].Controls[0];
                    target1.Text = DataBinder.Eval(item.DataItem, colNameArray[i]).ToString();
                }
            }
        }
    }
}
   




   

